#include "StdAfx.h"
#include "C3dsMgr.h"
#include "3ds.h"

C3dsMgr g_3dsMgr;
C3dsMgr::C3dsMgr(void)
{
}

C3dsMgr::~C3dsMgr(void)
{
}
C3dsMgr& C3dsMgr::Inst()
{
	return g_3dsMgr;
}
bool C3dsMgr::Load3dsFile(const char* szPath)
{
	TiXmlDocument doc;  
	if (!doc.LoadFile(szPath))
	{  	
		assert(0);
		return false;
	}	
	
	TiXmlElement* DlgMgrElem = doc.RootElement();	
	for (TiXmlElement* DlgElem = DlgMgrElem->FirstChildElement(); DlgElem!=NULL; DlgElem = DlgElem->NextSiblingElement())
	{
		map<tstring, tstring> mapAtt;
		for(const TiXmlAttribute *pAtt  = DlgElem->FirstAttribute(); pAtt!=NULL; pAtt = pAtt->Next())
		{
			pair<map<tstring, tstring>::iterator, bool> ret;
			ret = mapAtt.insert(make_pair(conversion::UTF8ToUnicode(pAtt->Name()), conversion::UTF8ToUnicode(pAtt->Value())));
			if (ret.second == false)
				assert(0);
		}
		Add3dsModel(mapAtt);
	}
	return true;
}
void C3dsMgr::Add3dsModel(map<tstring, tstring>& mapAtt)
{
	C3DS* p3ds = new C3DS;
	DWORD dwID = DW_INVALID;
	map<tstring, tstring>::iterator iter;
	iter = mapAtt.find(_T("id"));
	if (iter == mapAtt.end()) assert(0);
	dwID = conversion::Unicode2Int(iter->second.c_str());
	iter = mapAtt.find(_T("path"));
	if (iter == mapAtt.end())assert(0);
	const wchar_t* path = iter->second.c_str();

	iter = mapAtt.find(_T("picpath"));
	if (iter == mapAtt.end())assert(0);
	p3ds->Init(path, iter->second.c_str());

	pair<map<DWORD, C3DS*>::iterator,bool> ret;
	ret = m_map3dsModel.insert(make_pair(dwID, p3ds));
	if (ret.second == false) assert(0);
}

C3DS* C3dsMgr::Get3dsModel(DWORD dwID)
{
	map<DWORD, C3DS*>::iterator iter = m_map3dsModel.find(dwID);
	if (iter != m_map3dsModel.end())
	{
		return iter->second;
	}
	return NULL;
}

